<template>
  <div class="health">
    <breadcrumb title="健康打卡管理" homePath="/teacher" />
    <el-card>
      <h1 class="title">今日填表情况</h1>
      <el-row :gutter="10" class="head">
        <!-- 应填表人数 -->
        <el-col :span="6">
          <div>
            <i class="el-icon-document"></i>
            <h3>{{ allPeople }}</h3>
            <p>应填报人数</p>
            <p @click="gotoDetail(1)">
              More Info <i class="el-icon-arrow-right"></i>
            </p>
          </div>
        </el-col>
        <!-- 已填报人数 -->
        <el-col :span="6">
          <div>
            <i class="el-icon-s-data"></i>
            <h3>{{ submitPeople }}</h3>
            <p>已填报人数</p>
            <p @click="gotoDetail(2)">
              More Info <i class="el-icon-arrow-right"></i>
            </p>
          </div>
        </el-col>
        <!-- 风险人群 -->
        <el-col :span="6">
          <div>
            <i class="el-icon-s-opportunity"></i>
            <h3>{{ riskPeople }}</h3>
            <p>风险人群</p>
            <p @click="gotoDetail(3)">
              More Info <i class="el-icon-arrow-right"></i>
            </p>
          </div>
        </el-col>
        <!-- 经过高危地区人群 -->
        <el-col :span="6">
          <div>
            <i class="el-icon-warning"></i>
            <h3>{{ dangerPeople }}</h3>
            <p>经过高危地区人群</p>
            <p @click="gotoDetail(4)">
              More Info <i class="el-icon-arrow-right"></i>
            </p>
          </div>
        </el-col>
      </el-row>
      <el-row style="margin-top: 20px">
        <el-col :span="12">
          <div class="line1" :style="{ width: '100%', height: '400px' }"></div>
        </el-col>
        <el-col :span="12">
          <div class="line2" :style="{ width: '100%', height: '400px' }"></div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { optionDay, optionMonth } from "common/options";

import Breadcrumb from "components/Breadcrumb";

import { getUsersByTypePage } from "network/users/getUsersByTypePage";

import { gethealthNowDay } from "network/teacher/gethealthNowDay";
import { gethealthNowDayPage } from "network/teacher/gethealthNowDayPage";
import { gethealthNowMonth } from "network/teacher/gethealthNowMonth";
import { getAllHealth } from "network/teacher/getAllHealth";

export default {
  name: "Health",
  data() {
    return {
      allPeople: 0,
      allPeopleList: [],
      submitPeople: 0,
      submitPeopleList: [],
      riskPeople: 0,
      riskPeopleList: [],
      dangerPeople: 0,
      dangerPeopleList: [],
      lineChart1: {},
      lineChart2: {},
    };
  },
  components: {
    Breadcrumb,
  },
  async created() {
    this.$store.commit("setCurrentIndex", {
      type: "teacherCurrentIndex",
      index: 0,
    });
    // 获取学生总数
    this.getUsersByTypePage();
    // 获取填报学生
    this.gethealthNowDay();
    // 获取本月填报学生情况
    this.gethealthNowMonth();
  },
  mounted() {
    this.lineChart1 = this.$echarts.init(document.querySelector(".line1"));
    this.lineChart1.setOption(optionDay);

    this.lineChart2 = this.$echarts.init(document.querySelector(".line2"));
    this.lineChart2.setOption(optionMonth);
  },
  methods: {
    // 获取学生总数
    async getUsersByTypePage() {
      let res = await getUsersByTypePage(2, 1, 1);
      this.allPeople = res.total;
      this.allPeopleList = res.data;
    },
    // 获取今日填报学生总数
    async gethealthNowDay() {
      let res = await gethealthNowDay();
      this.submitPeopleList = res;
      this.submitPeople = res.length;

      // 统计时间点
      this.submitPeopleList.forEach((item) => {
        let hh = item.create_time.slice(11, 13);
        optionDay.series[0].data[parseInt(hh) + 8]++;
      });
      this.lineChart1.setOption(optionDay);

      // 获取风险和高危人群
      this.getRiskPeople();
      this.getDangerPeople();
    },
    // 获取风险人群
    getRiskPeople() {
      optionDay.series[0].data = new Array(24).fill(0);
      this.submitPeopleList.forEach((item) => {
        if (item.temperature > 37.3 || item.fever == "是") {
          this.riskPeople++;
          this.riskPeopleList.push(item);
        }
      });
    },
    // 获取高危人群
    getDangerPeople() {
      this.submitPeopleList.forEach((item) => {
        if (item.godanger == "是") {
          this.dangerPeople++;
          this.dangerPeopleList.push(item);
        }
      });
    },
    // 跳转详情页
    gotoDetail(index) {
      this.$router.push(`/teacher/detail/${index}`);
    },
    // 获取本月填表
    async gethealthNowMonth() {
      let res = await gethealthNowMonth();
      optionMonth.series[0].data = new Array(new Date().getDate() + 1).fill(0);
      console.log(res);
      res.forEach((item) => {
        let day = item.create_time.slice(8, 10);
        optionMonth.series[0].data[parseInt(day) - 1]++;
      });
      console.log(optionMonth);
      this.lineChart2.setOption(optionMonth);
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  .title {
    text-align: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
  }
  .el-row.head {
    color: white;
    .el-col div {
      position: relative;
      border-radius: 5px;
      > i {
        position: absolute;
        z-index: 9999;
        color: #eee;
        right: 20px;
        top: 20px;
        font-size: 60px;
      }
      h3 {
        padding: 10px 0 20px 10px;
      }
      p:nth-of-type(1) {
        padding: 0 0 25px 10px;
      }
      p:nth-of-type(2) {
        text-align: center;
        width: 100%;
        height: 30px;
        line-height: 30px;
        font-size: 12px;
        cursor: pointer;
        background-color: rgba(0, 0, 0, 0.2);
      }
    }
    .el-col:nth-child(1) div {
      background-color: #0a97bb;
    }
    .el-col:nth-child(2) div {
      background-color: #1e9543;
    }
    .el-col:nth-child(3) div {
      background-color: #ffbc00;
    }
    .el-col:nth-child(4) div {
      background-color: #ea4643;
    }
  }
}
</style>
